package fc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import connectionPool.PoolException;

import sql.SQLs;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		ResultSet rs = null;
		try {
			rs = SQLs.auswaehlen("Benutzername, Passwort, Leiter", "person");
			while(rs.next()){
				// Ueberprueft ob Benutzername und Kennwort uebereinstimmen
				if(request.getParameter("name").equals(rs.getString("Benutzername")) & request.getParameter("pw").equals(rs.getString("Passwort"))){
					session.setAttribute("eingeloggt", "true");
					session.setAttribute("Leiter", rs.getString("Leiter"));
				}
			}
		} catch (PoolException e) {		
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		response.sendRedirect("/SVNFeedbackTool/Main.jsp");

	}

}
